﻿/******************************************************************************
* File description: barCodeManager.cpp
* Function: 耗材编码管理
* Current Version: 1.0.0
* Author: JNN
* Date: 2025-07-01
*  -----
*  Copyright by 博坤生物 .
 *****************************************************************************/

#include "barCodeManager.h"


/** @brief 构造函数 */
BarCodeManager::BarCodeManager()
{

}


/** @brief 单例函数 */
BarCodeManager& BarCodeManager::I(){
    static BarCodeManager instance;
    return instance;
}


/** @brief 添加耗材编码
 *  @param 耗材编码
 *  @return QString 成功返回""，失败返回原因*/
QString BarCodeManager::addBarCode(QString code){

    __LOG_FSTART(code);

    QString checkCodeValid = isValidCode(code);
    if(checkCodeValid != ""){
        __LOG_FEND(checkCodeValid);
        return checkCodeValid;
    }

    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);

    // 检查数据库里面是否有该Code
    QString sql = QString("Select count(code) from BarCode where code='%1'").arg(code);
    query.exec(sql);
    while (query.next()) {      // 如果没有则添加
        if(query.value("Count(Code)").toInt() == 0){
            QString createDate = QDateTime::currentDateTime().toString("yyyy-MM-dd");
            int state = 1, times = 5;
            QString sql = QString("Insert Into BarCode (CreateDate, Code, State, Times) values('%1', '%2', '%3', '%4')").arg(createDate, code, QString::number(state), QString::number(times));
            query.exec(sql);
        }
    }
    __LOG_FEND("");
    return "";
}

/** @brief 获取所有的耗材编码列表
 *  @return QVariantList 耗材编码列表  */
QVariantList BarCodeManager::getBarCodeList(){

    __LOG_FSTART__;

    QVariantList res;

    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);

    QString sql = "Select * from BarCode";

    query.exec(sql);
    while (query.next()) {
        QVariantMap map;
        for(int i = 0; i < query.record().count(); ++ i){
            map[query.record().fieldName(i)] = query.value(i);
        }
        res.append(map);
    }
    __LOG_FEND("LogCount:"+QString::number(res.size()));
    return res;
}

/** @brief 删除指定的耗材编码
 *  @param code 耗材编码
 *  @return bool 是否删除成功  */
bool BarCodeManager::deleteBarCode(QString code){

    __LOG_FSTART(code);

    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql = QString("Delete From BarCode where code='%1'").arg(code);
    bool res = query.exec(sql);
    __LOG_FEND(res);
    return res;
}

/** @brief 根据指定耗材编码启动实验（该耗材编码将会消耗一次）
 *  @param code 耗材编码
 *  @return QString 成功返回""，失败返回原因  */
QString BarCodeManager::startExpWithCode(QString code){


    __LOG_FSTART(code);

    // 检查耗材编码是否有效
    QString checkCodeValid = isValidCode(code);
    if(checkCodeValid != ""){
        __LOG_FEND(checkCodeValid);
        return checkCodeValid;
    }

    // 如果当前数据库没有该耗材编码，重新插入一个
    addBarCode(code);

    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql = QString("update BarCode set times=times-1 where code='%1'").arg(code);
    query.exec(sql);

    __LOG_FEND("");
    return "";
}

/** @brief 根据指定耗材编码启动实验（该耗材编码将会消耗一次）
 *  @param code 耗材编码
 *  @return QString 成功返回""，失败返回原因  */
QString BarCodeManager::isValidCode(QString code){

    __LOG_FSTART(code);
    QString str = "1234.56";
    bool ok;
    double val = code.toDouble(&ok);

    QString validChar = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789_";

    if(code.size() < 5){
        __LOG_FEND("耗材编码无效");
        return "耗材编码无效";
    }
    auto a = code[0];
    for(int i = 0; i < code.size(); i ++){
        if(!validChar.contains(code[i])){
            __LOG_FEND("耗材编码无效");
            return "耗材编码无效";
        }
    }

    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql = QString("select times from barcode where code='%1'").arg(code);
    query.exec(sql);
    while(query.next()){
        if(query.value("times").toInt() == 0){
            __LOG_FEND("耗材编码超次");
            return "耗材编码超次";
        }
    }
    __LOG_FEND("");
    return "";
}


/** @brief 获取耗材编码总数
 *  @return int 耗材编码总数  */
int BarCodeManager::getBarCodeNum(){

    __LOG_FSTART__;
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql = "select count(id) from barcode";
    query.exec(sql);
    while(query.next()){
        int res = query.value("count(id)").toInt();
        __LOG_FEND(res);
        return res;
    }
    return 0;

}
